package com.faithbj.danaliverpool.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;

import com.faithbj.common.model.User;
import com.faithbj.common.utils.LogUtil;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 创建时间：2012-7-25 下午02:16:14 类说明
 */
public class LogoutFilter implements Filter
{
	private LogUtil logUtil;

	@Override
	public void destroy()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest hsrequest = (HttpServletRequest) request;
		if(hsrequest.getRequestURI().endsWith("/logout"))
		{
			Object userObject = null;
			if(SecurityContextHolder.getContext().getAuthentication() != null)
			{
				userObject = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			}
			if (userObject != null && userObject instanceof User)
			{
				logUtil.logUserLogInfo((User)userObject, "登出");
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{
		// TODO Auto-generated method stub

	}

	public LogUtil getLogUtil()
	{
		return logUtil;
	}

	public void setLogUtil(LogUtil logUtil)
	{
		this.logUtil = logUtil;
	}
}
